<?php

namespace app\admin\model;

use app\admin\model\CommonModel;
use think\Db;

class BatMatchModel extends CommonModel
{

    const EVEMT_STATUS_PENDING   = 0;
    const EVEMT_STATUS_START     = 1;
    const EVEMT_STATUS_BARREN    = 2;
    const EVEMT_STATUS_RESULTFUL = 3;

    public static $refundStatusMap = [
        self::EVEMT_STATUS_PENDING   => '未开始投注',
        self::EVEMT_STATUS_START     => '正在投注',
        self::EVEMT_STATUS_BARREN    => '投注结束未出结果',
        self::EVEMT_STATUS_RESULTFUL => '已出结果',
    ];

    const FOOTBALL   = 0;
    const BASKETBALL = 1;
    const ELECTRONIC = 2;

    public static $typeMap = [
        self::FOOTBALL   => '足球',
        self::BASKETBALL => '篮球',
        self::ELECTRONIC => '电竞',
    ];


    //获取器
    public function getMatchTimeAttr($value)
    {
        return date('m/d H:i', $value);
    }


    public function getBetEndTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }


    /**
     * @return \think\model\relation\BelongsTo
     * 获取主球队
     */
    public function teamMain()
    {
        return $this->belongsTo('TeamModel', 'team_main_id');
    }

    public function teamMainName()
    {
        return $this->belongsTo('TeamModel', 'team_main_id')->field('id,team_name,logo');
    }

    public function member()
    {
        return $this->belongsToMany('MemberModel', 'bat_order', 'member_id', 'bat_id');
    }


    /**
     * @return \think\model\relation\BelongsTo
     * 获取客球队
     */
    public function teamPass()
    {
        return $this->belongsTo('TeamModel', 'team_pass_id');
    }

    public function teamPassName()
    {
        return $this->belongsTo('TeamModel', 'team_pass_id')->field('id,team_name,logo');;
    }

    /**
     * @return \think\model\relation\HasOne
     * 猜让球
     */
    public function give()
    {
        return $this->hasOne('GiveModel', 'bat_id');
    }

    /**
     * @return \think\model\relation\HasOne
     * 猜大小球
     */
    public function size()
    {
        return $this->hasOne('SizeModel', 'bat_id');
    }

    /**
     * @return \think\model\relation\HasOne
     * 猜胜负
     */
    public function victory()
    {
        return $this->hasOne('VictoryModel', 'bat_id');
    }

    /**
     * @return \think\model\relation\HasMany
     * 获取投注记录
     */
    public function betOrder()
    {
        return $this->hasMany('BatOrderModel', 'bat_id');
    }

    /**
     * @return int 猜胜负
     */
    public function victory_result()
    {
        if ($this->main_score > $this->pass_score) {
            return 0; //主胜
        } elseif ($this->main_score == $this->pass_score) {
            return 1; //平
        } else {
            return 2;  //客胜
        }
    }

    /**
     * @param $bat
     * @return int 比赛结果
     *
     */
    public function size_result()
    {
        $size = Db::name('size')->where('bat_id', $this->id)->find();
        if (!$size) {
            return false;
        }
        $num = $size['s_size_num'];
        if (($this->pass_score + $this->main_score) > $num) {
            return 0; //大球
        } elseif (($this->pass_score + $this->main_score) == $num) {
            return 1;//平
        } else {
            return 2; //小球
        }
    }


    public function give_result()
    {
        $give = Db::name('give')->where('bat_id', $this->id)->find();
        if (!$give) {
            return false;
        }
        $num = $give['g_give_num'];
        if ($this->pass_score + $num > $this->main_score) {
            return 2;  // 客胜
        } elseif ($this->pass_score + $num == $this->main_score) {
            return 1; //平
        } else {
            return 0;  //主胜
        }
    }


}
